﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using Silvermoon.Controls;
using Passwords.Models;
using Silvermoon.UI;
using Passwords.Utils;
using Silvermoon.Navigation;
using Passwords.Controllers;
using Passwords.Controls;
using Passwords.Biz;

namespace Passwords.Pages
{
    /// <summary>
    /// Shows all categories to multiselect for a new password to create.
    /// </summary>
    public class NewPasswordCategoriesPage : Page
    {
        protected class CategorySelection
        {
            public Category Category;
            public bool IsSelected;
        }

        protected ListViewController<PickerItem, CategorySelection> controller;
        private ListView listView;
        private Toolbar toolbar;
        private CategorySelection[] selection;

        public NewPasswordCategoriesPage()
            : base()
        {
            InitializePage();
        }

        protected IEnumerable<CategorySelection> Selection { get { return selection; } }

        protected int SelectedCategories
        {
            get
            {
                if (selection == null) return 0;
                return selection.Count(x => x.IsSelected);
            }
        }

        private void InitializePage()
        {
            Title = "CHOOSE CATEGORIES";
            Style = StylePool.Window;
            SetTransition(350, TransitionMask.Fade | TransitionMask.Explode, TransitionMask.Fade | TransitionMask.Explode);
            listView = new ListView { Margin = Margin.Zero, Multiselect = true };
            Controls.Add(listView);
            controller = new ListViewController<PickerItem, CategorySelection>(listView);
            controller.UpdateItem += new EventHandler<ItemControlGeneratorEventArgs<PickerItem>>(controller_UpdateItem);
            controller.InitializeItem += new EventHandler<ItemControlGeneratorEventArgs<PickerItem>>(controller_InitializeItem);
            controller.DataSource = GetSource();

            toolbar = new Toolbar();
            toolbar.Buttons.Add(new Button { Content = "Select", Width = 180 });
            toolbar.Buttons.Add(new Button { Content = "Cancel", Width = 180, Command = Page.BackCommand });
            toolbar.Buttons[0].Tap += new EventHandler<TapEventArgs>(OnSelect);
            Toolbar = toolbar;
        }

        protected override void OnInitialized()
        {
            toolbar.Buttons[0].Enabled = false;
            base.OnInitialized();
        }


        protected virtual void OnSelect(object sender, EventArgs e)
        {
            var selectedCategories = from c in selection where c.IsSelected == true select c.Category;

            Password password = new Password {  };
            foreach (var c in selectedCategories)
            {
                password.Categories.Add(c);
            }
            EditDetailsPage page = new EditDetailsPage { Password = password };
            //NavigateTo(page, PageTransitions.DetailsEditFlip);
            this.NavigateTo(page);
            RemoveFromStack();
        }

        protected virtual IEnumerable<CategorySelection> GetSource()
        {
            selection = (from c in Context.Current.Categories select new CategorySelection { Category = c, IsSelected = false }).ToArray();
            return selection;
        }

        void controller_InitializeItem(object sender, ItemControlGeneratorEventArgs<PickerItem> e)
        {
            PickerItem item = e.Item;
            item.TitleFont = FontCache.BigRegularFont;
            item.Height = 100;
            item.SelectionChanged += OnItemSelectionChanged;
        }

        void OnItemSelectionChanged(object sender, EventArgs e)
        {
            PickerItem item = sender as PickerItem;
            if (item.Host != null)
            {
                CategorySelection category = item.DataValue as CategorySelection;
                category.IsSelected = item.IsSelected;
                toolbar.Buttons[0].Enabled = SelectedCategories > 0;
            }
        }

        protected override void OnShown()
        {
            base.OnShown();
        }

        void controller_UpdateItem(object sender, ItemControlGeneratorEventArgs<PickerItem> e)
        {
            PickerItem item = e.Item;
            CategorySelection c = e.Value as CategorySelection;
            item.IsSelected = c.IsSelected;
            item.Title = c.Category.Name;
        }
    }
}
